package za.org.jcicada.syshealth;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.AbstractListModel;

import za.org.jcicada.genss.sensor.ISensor;

public class SensorModel extends AbstractListModel implements Observer {

	private static final long serialVersionUID = 1L;
	private List<ISensor> sensors = new ArrayList<ISensor>();

	public void addElement(ISensor sensor) {
		if (!sensors.contains(sensor)) {
			sensor.addObserver(this);
			sensors.add(sensor);
			fireContentsChanged(this, sensors.size() - 1, sensors.size());
		}
	}
	
	public Object getElementAt(int index) {
		return sensors.get(index);
	}

	public int getSize() {
		return sensors.size();
	}
	
	public void clear() {
		int previousSize = getSize();
		for (ISensor sensor : sensors) {
			sensor.deleteObserver(this);
		}
		sensors.clear();
		fireIntervalRemoved(this, 0, previousSize);
	}

	public void update(Observable arg0, Object arg1) {
		int index = sensors.indexOf(arg0);
		fireContentsChanged(this, index, index);
	}

}
